﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ContactListClientBasic
{
    public partial class Form1 : Form
    {
        MyService.Contact[] contacts;
        MyService.User user;

        String logOut = "Log out";
        String logIn = "Log In";

        public Form1()
        {
            InitializeComponent();
            button1.Text = logIn;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox2.Items.Clear();
            listBox3.Items.Clear();

            int id = contacts[listBox1.SelectedIndex].id;
            MyService.ContactDetail contactDetail = mGetService().GetContactDetail(id,user.token);


            tbxTitlename.Text = contactDetail.titlename;
            tbxFirstname.Text = contactDetail.firstname;
            tbxLastname.Text = contactDetail.lastname;
            tbxNickname.Text = contactDetail.nickname;
            tbxAddress.Text = contactDetail.address.address;
            tbxCountry.Text = contactDetail.address.country;
            tbxZipcode.Text = contactDetail.address.Zipcode;
            pictureBox1.Image = Base64ToImage(contactDetail.image);

            for (int i = 0; i < contactDetail.telephone.Length; i++)
            {
                listBox3.Items.Add(contactDetail.telephone[i].number);
            }

            for (int i = 0; i < contactDetail.email.Length; i++)
            {
                listBox2.Items.Add(contactDetail.email[i]);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text.Equals(logIn))
            {
                user = mGetService().GetUser();
                if (user == null)
                {
                    MessageBox.Show("ชื่อผู้ใช้ หรือ รหัสผ่าน ไม่ถูกต้อง");
                }
                else
                {
                    button1.Text = logOut;
                    tbxUsername.Enabled = false;
                    tbxPassword.Enabled = false;
                    lblLoginStatus.Text = "Login ON : " + user.name;

                    contacts = mGetService().GetContactList(user.id, user.token);
                    for (int i = 0; i < contacts.Length; i++)
                    {
                        listBox1.Items.Add(contacts[i].firstname + " " + contacts[i].lastname);
                    }

                    listBox1.Enabled = true;
                    listBox2.Enabled = true;
                    listBox3.Enabled = true;
                }
            }else if(button1.Text.Equals(logOut))
            {
                button1.Text = logIn;
                lblLoginStatus.Text = "Login OFF";
                tbxUsername.Enabled = true;
                tbxUsername.Text = "";
                tbxPassword.Enabled = true;
                tbxPassword.Text = "";
                user = null;

                contacts = null;
                listBox1.Items.Clear();
                listBox2.Items.Clear();
                listBox3.Items.Clear();

                listBox1.Enabled = false;
                listBox2.Enabled = false;
                listBox2.Enabled = false;

                tbxTitlename.Text = "";
                tbxFirstname.Text = "";
                tbxLastname.Text = "";
                tbxNickname.Text = "";
                tbxAddress.Text = "";
                tbxCountry.Text = "";
                tbxZipcode.Text = "";
                pictureBox1.Image = null;
            }
        }

        private MyService.ContactService mGetService()
        {
            MyService.AuthHeader auth = new MyService.AuthHeader();
            auth.Username = tbxUsername.Text;
            auth.Password = tbxPassword.Text;

            MyService.ContactService service = new MyService.ContactService();
            service.AuthHeaderValue = auth;
            return service;
        }

        private void lblLoginStatus_Click(object sender, EventArgs e)
        {
            if (user != null)
            {
                MessageBox.Show("");
            }
        }


        public Image Base64ToImage(string base64String)
        {
            // Convert Base64 String to byte[]
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0,
              imageBytes.Length);

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            Image image = Image.FromStream(ms, true);
            return image;
        }
    }
}
